package com.solr.web.controller.search;

import org.apache.solr.client.solrj.SolrServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solr.web.model.base.ResultModel;
import com.solr.web.service.search.SearchService;

@RequestMapping("/import")
@Controller
public class ImportController {

	@Autowired
	private SearchService service;

	@Autowired
	private SolrServer solrServer;
	
	/**
	 * 进入页面
	 * @return
	 */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view() {
        return "search/import-index";
    }
	
    /**
     * 全量导入索引库
     * @return
     */
	@RequestMapping("/importAll")
	@ResponseBody
	public ResultModel importAll() {
		ResultModel result = new ResultModel();
		service.importAll();
		 return result;
	}

	/**

	 * 增量导入索引库

	 * @return

	 */
	@RequestMapping("/importWhere")
	@ResponseBody
	public ResultModel importWhere(String date) {
		ResultModel result = new ResultModel();
		
		//SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//String dateString = formatter.format(date);
		//System.out.println("==========================="+dateString);
		service.importWhere(date);
		return result;
	}

	
	 /** 
     * 清空索引库 
     * @param scheme 
     * @throws Exception 
     */  
	@RequestMapping("/delete")
    public void deleteAllIndexs() throws Exception {  
        solrServer.deleteByQuery("*:*");  
        solrServer.commit();  
    } 
	
}